package lili.com.my.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;

import lili.com.my.R;
import lili.com.my.activity.ListNews;
import lili.com.my.activity.NewsActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExerciseFragment extends Fragment {
    RecyclerView recyclerView;
    ArrayList<ListNews> lists=new ArrayList<>();
    Gson gson;

    public ExerciseFragment() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exercise, container, false);
        recyclerView = view.findViewById(R.id.rv);
        loading();
        return view;
    }

    public void loading() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd","7");
        params.addQueryStringParameter("v1","274");
        params.addQueryStringParameter("v2","1");
        params.addQueryStringParameter("v3","30");
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
              String result1=  convertString(result);
              Type listType=new TypeToken<ArrayList<ListNews>>(){

              }.getType();
              if (gson==null){
                  gson=new Gson();
              }
              lists=gson.fromJson(result1,listType);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.HORIZONTAL));
              recyclerView.setAdapter(new MyAdatper());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");
        return result1;

    }
    public class MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(getActivity(),R.layout.item_exercise,null);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
                final ListNews listNews=lists.get(position);
                holder.textView.setText(listNews.contentTitle);
                holder.timeView.setText(listNews.submitTime);
                holder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(getActivity(), NewsActivity.class);
                        intent.putExtra("contentid",listNews.contentID);
                        startActivity(intent);
                    }
                });
        }

        @Override
        public int getItemCount() {
            return lists.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        TextView timeView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.item_tv);
            timeView=itemView.findViewById(R.id.date);
        }
    }
}
